/*
 * Copyright (c) 2009-2012 Panxiaobo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package p.cod;

public class Xdump implements Opcodes {
    int opcode, offset, base;

    public Xdump(int base) {
        super();
        this.base = base;
    }

    public void update(int offset, int opcode) {
        this.offset = offset;
        this.opcode = opcode;
    }

    void out(String s) {
        out(s, "");
    }

    public void sb(int a, int b) {
        out(String.format("%04x %02x", a, b));
    }

    public void bs(int a, int b) {
        out(String.format("%02x %04x", a, b));
    }

    public void s(int a) {
        out(String.format("%04x", a));
    }

    public void sbs(int a, int b, int c) {
        out(String.format("%04x %02x %04x", a, b, c));
    }

    public void bsb(int a, int b, int c) {
        out(String.format("%02x %04x %02x", a, b, c));
    }

    public void bbb(int a, int b, int c) {
        out(String.format("%02x %02x %02x", a, b, c));
    }

    public void b(int a) {
        out(String.format("%02x", a));
    }

    public void x() {
        out("");
    }

    public void i(int a) {
        out(String.format("%08x", a));
    }

    public void l(long a) {
        out(String.format("%016x", a));
    }

    public void ss(int b, int a) {
        out(String.format("%04x %04x", a, b));
    }

    public void bss(int a, int b, int c) {
        out(String.format("%02x %04x %04x", a, b, c));
    }

    public void bbs(int a, int b, int c) {
        out(String.format("%02x %02x %04x", a, b, c));
    }

    public void bb(int a, int b) {
        out(String.format("%02x %02x", a, b));
    }

    public void bbbb(int a, int b, int c, int d) {
        out(String.format("%02x %02x %02x %02x", a, b, c, d));
    }

    public void lsb(int lib, int a, int b) {
        if (lib == 0) {
            sb(a, b);
            return;
        } else {
            bsb(lib, a, b);
        }
    }

    public void lbs(int lib, int a, int b) {
        if (lib == 0) {
            bs(a, b);
            return;
        } else {
            bbs(lib, a, b);
        }
    }

    public void lb(int lib, int a) {
        if (lib == 0) {
            b(a);
            return;
        } else {
            bb(lib, a);
        }
    }

    public void lbbb(int lib, int a, int b, int c) {
        if (lib == 0) {
            bbb(a, b, c);
            return;
        } else {
            bbbb(lib, a, b, c);
        }
    }

    public void lbb(int lib, int a, int b) {
        if (lib == 0) {
            bb(a, b);
            return;
        } else {
            bbb(lib, a, b);
        }
    }

    public void lsb(int lib, int a, int b, Object ro) {
        if (lib == 0) {
            out(String.format("%04x %02x", a, b), ro);
        } else {
            out(String.format("%02x %04x %02x", lib, a, b), ro);
        }
    }

    private void out(String s, Object ro) {
        if (opcode <= 0xff) {
            System.out.println(String.format("%08x |%04x| %02x %-16s |%s %s", base + offset, offset, opcode, s,
                    OPCODE_NAMES[opcode], ro));
        } else {
            System.out.println(String.format("%08x |%04x| %04x %-14s |%s %s", base + offset, offset, opcode, s,
                    OPCODE_NAMES[opcode], ro));
        }
    }

    public void s(int a, Object string) {
        out(String.format("%04x", a), string);
    }

    public void lbs(int lib, int a, int b, Object obj) {
        if (lib == 0) {
            out(String.format("%02x %04x", a, b), obj);
        } else {
            out(String.format("%02x %02x %04x", lib, a, b), obj);
        }
    }

    public void lb(int lib, int a, Object obj) {
        if (lib == 0) {
            out(String.format("%02x", a), obj);
        } else {
            out(String.format("%02x %02x", lib, a), obj);
        }
    }
}
